﻿Imports System.Data.SqlClient
Public Class frmEmployee_Editter

    Private Sub frmEmployee_Editter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnClose.Text = "បិទ"
        If txtemp_id.Text = "" Then
            btnSave.Text = "រក្សាទុក"
        Else
            btnSave.Text = "កែប្រែ"
        End If
    End Sub

    Private Sub txtcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcode.TextChanged
        txtcode.BackColor = Color.White
        If txtcode.Text = "" Then
            btnClose.Text = "បិទ"
        Else
            btnClose.Text = "សំអាត"
        End If
    End Sub
    Public Sub clear()
        txtcode.Clear()
        txtE_name.Clear()
        txtAdr.Clear()
        txtPhone.Clear()
        txtEmail.Clear()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If btnClose.Text = "បិទ" Then
            Me.Close()
        Else
            Me.clear()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtcode.Text = "" Then
            MsgBox("Text box code is Emty")
            txtcode.BackColor = Color.PeachPuff
            Exit Sub
        ElseIf txtE_name.Text = "" Then
            MsgBox("Text box Name is Emty")
            txtE_name.BackColor = Color.PeachPuff
            Exit Sub
        Else
            Dim sql As String
            If txtemp_id.Text = "" Then
                sql = "insert into tblemployee values (@em_code, @emp_name, @dob, @pro_id, @adr, @phon, @email, @pos_id, @s_id, @photo);"
            Else
                sql = "update tblemployee set (@em_code,@emp_name,@dob,@pro_id, @adr, @phon, @email, @pos_id, @s_id, @photo where em_id=@em_id);"
            End If
            con.Open()
            Dim cm As New SqlCommand(sql, con)
            cm.Parameters.Add("@em_code", SqlDbType.NVarChar).Value = Me.txtcode.Text
            cm.Parameters.Add("@emp_name", SqlDbType.NVarChar).Value = Me.txtE_name.Text
            cm.Parameters.Add("@dob", SqlDbType.Date).Value = Me.dtpDOB.Value
            cm.Parameters.Add("@pro_id", SqlDbType.Int).Value = Me.txtpro_id.Text
            cm.Parameters.Add("@adr", SqlDbType.NVarChar).Value = Me.txtAdr.Text
            cm.Parameters.Add("@pos_id", SqlDbType.Int).Value = Me.txtposi_id.Text
            cm.Parameters.Add("@phone", SqlDbType.NVarChar).Value = Me.txtPhone.Text
            cm.Parameters.Add("@email", SqlDbType.NVarChar).Value = Me.txtpro_id.Text
            cm.Parameters.Add("@s_id", SqlDbType.Int).Value = Me.txtsex_id.Text
            If Me.picPhoto.Image Is Nothing Then
                cm.Parameters.Add("@PHOTO", SqlDbType.Image).Value = DBNull.Value
            Else
                Dim m As New System.IO.MemoryStream()
                Me.picPhoto.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
                Dim bytes As Byte() = m.GetBuffer()

                cm.Parameters.Add("@PHOTO", SqlDbType.Image).Value = bytes
            End If
            cm.ExecuteNonQuery()
            Me.Close()
            frmEmployee.load_data()
        End If
    End Sub

    Private Sub btnBrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrow.Click
        Dim diag As New OpenFileDialog()
        diag.Filter = "Image file |*.jpg|Bitmap Picture|*.bmp"
        If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim fileName = diag.FileName
            Me.picPhoto.Image = Image.FromFile(fileName)
        End If
    End Sub

    Private Sub btnClearImag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearImag.Click
        picPhoto.Image = Nothing
    End Sub

    Private Sub btnCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapture.Click
        Dim diag As New SimpleCam.FormCamera(True, False, False)
        If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
            picPhoto.Image = diag.ClippedImage
        End If
    End Sub

    Private Sub txtE_name_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtE_name.TextChanged
        txtE_name.BackColor = Color.White
    End Sub
End Class